<template>
  <div class="changeUser">
    <div class="header">
      <h1>用户管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">用户管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <el-button
          type="info"
          icon="el-icon-arrow-left"
          size="mini"
          @click="goBack"
        >
          返回
        </el-button>
      </div>
      <el-form
        size="small"
        class="changeForm"
        ref="changeForm"
        :model="changeForm"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="username">
              <span slot="label">
                <span class="labelText">用户名（必填）</span>
              </span>
              <el-input
                v-model="changeForm.username"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="realName">
              <span slot="label">
                <span class="labelText">真实姓名（必填）</span>
              </span>
              <el-input
                v-model="changeForm.realName"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="email">
              <span slot="label">
                <span class="labelText">Email</span>
              </span>
              <el-input
                v-model="changeForm.email"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="degree">
              <span slot="label">
                <span class="labelText">学历</span>
              </span>
              <el-input
                v-model="changeForm.degree"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="idNumber">
              <span slot="label">
                <span class="labelText">身份证号</span>
              </span>
              <el-input
                v-model="changeForm.identityNum"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="departmentId">
              <span slot="label">
                <span class="labelText">部门</span>
              </span>
              <el-select
                v-model="changeForm.departmentId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in depArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="occupationId">
              <span slot="label">
                <span class="labelText">职位</span>
              </span>
              <el-select
                v-model="changeForm.occupationId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in posArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="salary">
              <span slot="label">
                <span class="labelText">工资</span>
              </span>
              <el-input
                v-model="changeForm.salary"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="birthday">
              <span slot="label">
                <span class="labelText">生日</span>
              </span>
              <el-input
                placeholder="1980-01-01"
                v-model="changeForm.birthday"
                :validate-event="false"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="phoneNum">
              <span slot="label">
                <span class="labelText">电话</span>
              </span>
              <el-input
                v-model="changeForm.phoneNum"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="nickname">
              <span slot="label">
                <span class="labelText">昵称</span>
              </span>
              <el-input
                v-model="changeForm.nickname"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="address">
              <span slot="label">
                <span class="labelText">地址</span>
              </span>
              <el-input
                v-model="changeForm.address"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <span slot="label">
                <span class="labelText">密码</span>
              </span>
              <el-input
                v-model="changeForm.password"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="graduateSchool">
              <span slot="label">
                <span class="labelText">毕业院校</span>
              </span>
              <el-input
                v-model="changeForm.graduateSchool"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="bankAccount">
              <span slot="label">
                <span class="labelText">银行账号</span>
              </span>
              <el-input
                placeholder="工行卡"
                v-model="changeForm.bankAcount"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <el-form-item prop="sex">
              <span slot="label">
                <span class="labelText">性别</span>
              </span>
              <el-select
                v-model="changeForm.sex"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in sexArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="roleName">
              <span slot="label">
                <span class="labelText">角色</span>
              </span>
              <el-select
                v-model="changeForm.roleId"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in occupationArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="joinTime">
              <span slot="label">
                <span class="labelText">入职时间</span>
              </span>
              <el-input
                placeholder="2023-02-17"
                v-model="changeForm.joinTime"
                :validate-event="false"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item prop="skin">
              <span slot="label">
                <span class="labelText">皮肤</span>
              </span>
              <el-select
                v-model="value"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in skinArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item> -->
          </el-col>
        </el-row>
        <el-form-item class="btnBox">
          <el-button
            type="primary"
            size="small"
            @click="($event) => changeUser('changeForm')"
            >保存</el-button
          >
          <el-button type="info" size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "",

  data() {
    return {
      depArr: [],
      posArr: [],
      sexArr: [
        {
          value: "0",
          label: "女",
        },
        {
          value: "1",
          label: "男",
        },
      ],
      occupationArr: [],
      // skinArr:[],
      changeForm: {
        username: "",
        realName: "",
        email: "",
        degree: "",
        identityNum: "",
        departmentId: "",
        departmentName: "",
        money: "",
        salary: "",
        phoneNum: "",
        nickname: "",
        address: "",
        graduateSchool: "",
        bankAcount: "",
        sex: "",
        occupationId: "",
        occupationName: "", //职位
        joinTime: "",
        colorName: "",
        roleId: "",
        roleName: "", //角色
        salary: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "不能为空", trigger: "blur" }],
        nickname: [{ required: true, message: "不能为空", trigger: "blur" }],
        realName: [{ required: true, message: "不能为空", trigger: "blur" }],
        sex: [{ required: true, message: "不能为空", trigger: "blur" }],
        nickname: [{ required: true, message: "不能为空", trigger: "blur" }],
        identityNum: [{ required: true, message: "不能为空", trigger: "blur" }],
        password: [{ required: true, message: "不能为空", trigger: "blur" }],
        roleId: [{ required: true, message: "不能为空", trigger: "blur" }],
        occupationId: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        departmentId: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        joinTime: [
          {
            required: true,
            message: "不能为空且必须按照规定格式输入",
            trigger: "blur",
          },
        ],
      },
      value: "",
    };
  },

  mounted() {
    if (this.$route.params.row.sex == 1) {
      this.$route.params.row.sex = "男";
      this.changeForm = this.$route.params.row;
    } else {
      this.$route.params.row.sex = "女";
      this.changeForm = this.$route.params.row;
    }
    /* console.log(this.changeForm.sex);
    console.log(typeof(this.changeForm)); */
  },
  created() {
    this.getDepArr();
    this.getPosArr();
    this.getOccupationArr();
  },

  methods: {
    goBack() {
      this.$router.push("/user/userManage");
    },
    //改变部门
    /* async changeUser() {
      if (this.changeForm.sex == "女") {
        this.changeForm.sex = "0";
      } else {
        this.changeForm.sex = "1";
      }
      console.log(this.changeForm.sex);
      let res = await this.$api.user.changeUser(this.changeForm);
      if (res.data.code === 200) {
        //跳转到列表页面
        this.$router.push("/user/userManage");
        //提示
        this.$message({
          message: "添加部门成功",
          type: "success",
        });
      } else {
        //错误信息提示
        this.$message.error("出现错误，添加部门失败");
      }
    }, */
    changeUser(changeForm) {
      this.$refs[changeForm].validate((valid) => {
        if (valid) {
          if (this.changeForm.sex == "女") {
            this.changeForm.sex = "0";
          } else {
            this.changeForm.sex = "1";
          }
          this.$api.user.changeUser(this.changeForm).then((res) => {
            if (res.data.code === 200) {
              //跳转到列表页面
              this.$router.push("/user/userManage");
              //提示
              this.$message({
                message: "添加部门成功",
                type: "success",
              });
            } else {
              //错误信息提示
              this.$message.error("出现错误，添加部门失败");
            }
          });
        } else {
          this.$message.error("请填写必填项");
        }
      });
    },
    //取消按钮
    cancel() {
      this.$router.push("/user/userManage");
    },
    //获取部门选项
    async getDepArr() {
      let res = await this.$api.user.getDeptList1();
      let arrData = res.data.data;
      arrData.forEach((element) => {
        this.depArr.push({ label: element.depName, value: element.depId });
      });
    },
    //获取角色选项
    async getPosArr() {
      let res = await this.$api.user.getPosList1();
      let arrData = res.data.data.list;
      arrData.forEach((element) => {
        this.posArr.push({ label: element.title, value: element.id });
      });
    },
    //获取职业选项
    async getOccupationArr() {
      let res = await this.$api.user.getOccupationList1();
      // console.log(res);
      let arrData = res.data.data.list;
      arrData.forEach((element) => {
        this.occupationArr.push({ label: element.roleName, value: element.id });
        // console.log(element.id);
      });
    },
    /* //获取性别选项
    async getSexArr(){
      let res = await this.$api.user.getOccupationList1()
      // console.log(res);
      let arrData = res.data.data.list
      arrData.forEach(element => {
        this.occupationArr.push({label:element.roleName,value:element.id})
        // console.log(element.id);
      })
    }, */
  },
};
</script>

<style lang="less" scoped>
html body {
  height: 100%;
  background-color: #f5f5f5;
}
.addDep {
  height: 100%;
}
.header {
  position: relative;
  padding: 15px 15px 0 15px;
  display: flex;
  justify-content: space-between;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 500;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}
.content {
  padding: 15px;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.labelText {
  color: #333333;
  font-size: 14px;
}
.labelText2 {
  color: #999999;
  font-size: 12px;
  margin-left: 5px;
}
.addForm {
  background-color: #fff;
  padding: 5px;
}
</style>
